package com.qf.controller;

import com.netflix.discovery.EurekaClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.qf.entity.User;
import com.qf.feign.Feign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author Ypl
 * @version V1.0
 * @Project OctoberAndNovember
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/11/2 10:41
 */
@SuppressWarnings("all")
@RestController
public class OrderController {

    @Autowired
    EurekaClient eurekaClient;

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    Feign feign;

//    @Autowired
//    LoadBalancerClient loadBalancerClient;

    @RequestMapping("/api/query")
    /**
     * 配置线程隔离
     */
//    @HystrixCommand(fallbackMethod = "fallback",commandProperties = {
//            @HystrixProperty(name = "execution.isolation.strategy",value = "THREAD"),
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1000"),
//            @HystrixProperty(name = "execution.timeout.enabled",value = "true")
//
//    })

    /**
     * 配置断路器
     */
    @HystrixCommand(fallbackMethod = "fallback",commandProperties = {
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000")
    })
    public String queryOrder(){
        //通过Eureka注册中心的容器名称获取容器对象
//        InstanceInfo in = eurekaClient.getNextServerFromEureka("QF-GOODS", false);
//        //获取该容器对象的url
//        String url = in.getHomePageUrl();
//        System.out.println(url);

        //获取响应实体对象
//        ResponseEntity<String> responseEntity = restTemplate.getForEntity(url+"/query", String.class);
//        String body = responseEntity.getBody();
//        ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://QF-GOODS/query", String.class);
//        String body = responseEntity.getBody();

//        String body = feign.queryGoods();
//        String haonigexiaohuozi = feign.queryGoods2(2, "haonigexiaohuozi");
//        String s = feign.queryGoods3(222);

        User user = new User();
        user.setId(1314);
        user.setName("zhangsangnifg");
        user.setAge(23);
        String s = feign.queryGoods4(user);

        return "接收到的消息 :: "+ s;
    }

    public String fallback(){
        return "服务降级了";
    }
}
